<?php



function setGpsCmd($fd){
	$f = file('GPSCMD.cd');
	var_dump($fd);
	foreach($f as $k=>$v){
		if(strlen($v)<3 || substr($v,0,1)=='#'){
			continue;
		}
		$v = trim($v);
		//$v = "\x".str_replace(' ','\x',$v)."\r\n";
		$v = hex2bin(str_replace(' ', '',$v));
		dio_write($fd,$v);
		usleep(200000);
		echo $v;
		echo "\r\n";
	}
}


function conn_com($fd ,$com_name,$baud=9600,$data = 8,$stop = 1)
{
    if($fd) return $fd;
   
    if(stripos(php_uname(),'windows')>-1){
        try {
            @exec("mode $com_name: baud=$baud data=$data stop=$stop parity=n xon=on");
            $fd = dio_open("$com_name:",O_RDWR);
        } catch(Exception $e) {
            echo "$com_name conn field \r\n";
            return false;
        }
    }else{
        try{
            $fd = @dio_open($com_name, O_RDWR);
            if(!$fd) return false;
            //dio_fcntl($fd, F_SETFL, O_SYNC);
            dio_tcsetattr($fd, array(
                'baud' => 9600,
                'bits' => 8,
                'stop'  => 1,
                'parity' => 0
            ));
        } catch(Exception $e){
            echo "$com_name conn field \r\n";
            return false;
        }
    }

    echo "$com_name success\r\n";
    return $fd;
}

#@exec("mode COM4: baud=9600 data=8 stop=1 parity=n xon=on");
#$fd = dio_open("COM4",O_RDWR);
$fd = conn_com(false,'/dev/ttyS4');
setGpsCmd($fd);

//exit;

var_dump($fd);
while (1) {
    //dio_write($fd,"?T\r\n");
     $data = dio_read($fd);
	 echo $data;
    // $data = floatval(substr($data,2));
    // if ($data) {
        // echo $data;
    // }
    echo "\n\r";
	sleep(1);

}


